package com.tang.service.impl;

import com.tang.entity.Book;
import com.tang.entity.Borrow;
import com.tang.entity.Review;
import com.tang.repository.BorrowRepository;
import com.tang.repository.impl.BookRepositoryImpl;
import com.tang.repository.impl.BorrowRepositoryImpl;
import com.tang.service.BookService;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * @author tang
 * @create 2020-08-17
 */

/**
 * 处理借书的具体业务
 */
public class BookServiceImpl implements BookService {

    BookRepositoryImpl bookRepository =  new BookRepositoryImpl();
    BorrowRepository borrowRepository = new BorrowRepositoryImpl();

    @Override
    public int findAll(int limit) {
        int count = bookRepository.findAll();
        int pages;
        if (count / limit != 0) {
            pages = count/limit+1;
        }
        else {
            pages = count/limit;
        }
        return pages;
    }

    @Override
    public List<Book> find(int index,int limit) {
        List<Book> list = bookRepository.find(index,limit);
        return list;
    }

    @Override
    public void addBorrow(int readerId, int bookId) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String borrowTime = simpleDateFormat.format(date);

        Calendar calendar = Calendar.getInstance();
        int dates = calendar.get(Calendar.DAY_OF_YEAR)+14;
        calendar.set(Calendar.DAY_OF_YEAR,dates);
        Date date2 = calendar.getTime();
        String returntime  = simpleDateFormat.format(date2);

        borrowRepository.addborrow(readerId,bookId,borrowTime,returntime);

    }

    @Override
    public List<Borrow> query(int readerId) {
        return borrowRepository.query(readerId);
    }

    @Override
    public List<Review> reviewQuery() {
        return borrowRepository.reviewQuery();
    }

    @Override
    public List<Review> returnQuery() {
        return borrowRepository.returnQuery();
    }

    @Override
    public void hand(int id, int adminid, int state) {
        borrowRepository.handle(id, adminid, state);
    }
}
